<template>
  <Modal v-model="visible" title="年底奖金明细" :closable="false" :mask-closable="false" :width="80">
    <dt-info :line-number='3' :label-width="110" ref='info'></dt-info>
    
    <dt-grid ref="table" :list='list' :height='450' />
    
    <reason-modal ref='reason' />
        
    <div slot="footer">
      <Button @click="hide">关闭</Button>
      <Button type="primary" v-if="name == 'yearendEdit'" :disabled="disabled" @click="onOk">保存</Button>
    </div>
  </Modal>
</template>

<script>
  import DetailOpt from '../opt/DetailOpt.js'
  import DetailColumn from '../opt/DetailColumn.js'
  
  import reasonModal from './reason-modal.vue'
  import yearMgr from '@/biz/bonus/yearend'

  export default {
    components: {
      reasonModal
    },
    data() {
      return {
        visible: false,
        name: '',
        info: {},
        list: [],
        disabled: false
      }
    },
    methods: {
      onOk() {
        if (this.onValidate()) {
          this.disabled = true
          yearMgr.updateDetail({
            bonusDetailId: this.info.bonusDetailId,
            bonusList: this.list.map(i => ({
              bonusDetailId: i.bonusDetailId,
              bonusDetailYearId: i.bonusDetailYearId,
              adjustAmount: i.adjustAmount,
              adjustReason: i.adjustReason,
              oaNubmber: i.oaNubmber,
              adjustBonusPool: i.adjustBonusPool
            }))
          }).then(res => {
            this.disabled = false
            this.$Message.success('修改成功')
            this.$emit('success')
            this.hide()
          }).catch(() => {
            this.disabled = false
          })
        }
      },
      // 验证(“系统调整值”不为0且“年底调整金额”不为0)或者(当前为离职状态且“年底应发奖金”不为0)时，oa文电编号是否已填
      onValidate() {
        let err = true
        for (let item of this.list) {
          let systemAdjust = this.toNumber(item.systemAdjust),
              adjustAmount = this.toNumber(item.adjustAmount),
              shouldYearBonusAmount = this.toNumber(item.shouldYearBonusAmount)
          if (
            (systemAdjust != 0 && adjustAmount != 0 && !item.oaNubmber)
            || (this.info.jobStatus == 0 && shouldYearBonusAmount != 0 && !item.oaNubmber)
          ) {
            err = false
            this.$Message.error('离职人员发放年底奖金需填写OA文电编号')
            break
          }
        }
        return err
      },
      change(row) {
        let update = () => {
          // [年底应发奖金]=年底结算奖金+系统调整值+[年底调整金额]
          row.shouldYearBonusAmount = this.toNumber(row.yearSettlementAmount) + this.toNumber(row.systemAdjust) + this.toNumber(row.adjustAmount)
          // [年底系统计提]=[年底应发奖金]*2% (月度计提不为0时，计算年底系统计提)
          if (row.bonusPool && row.bonusPool != 0) {
            row.systemBonusPool = this.toNumber(row.shouldYearBonusAmount) * 0.02
          }
          // [年底实际计提]=[年底系统计提]+[年底计提调整]
          row.actualBonusPool = this.toNumber(row.systemBonusPool) + this.toNumber(row.adjustBonusPool)
          // [年底实发]=[年底应发奖金]-[年底实际计提]
          row.actualBonusAmount = this.toNumber(row.shouldYearBonusAmount) - this.toNumber(row.actualBonusPool)
          
          // 更新数据
          let idx = this.list.findIndex(i => i.bonusDetailYearId == row.bonusDetailYearId)
          this.list.splice(idx, 1, row)
          
          // 更新年底应发奖金总额
          this.updateShouldAmount()
        }
        if (this.toNumber(row.adjustAmount) != 0) {
          if (!row.adjustReason) {
            this.$refs.reason.show((reason) => {
              if (reason) {
                row.adjustReason = reason
              } else {
                row.adjustAmount = 0
              }
              update()
            })
            return
          }
        }
        update()
      },
      updateShouldAmount() {
        let amount = this.list.reduce((num, { shouldYearBonusAmount }) => {
          return num + this.toNumber(shouldYearBonusAmount)
        }, 0)
        this.info.shouldBonusAmount = amount
      },
      toNumber(val) {
        return val ? Number(val) : 0
      },
      getList() {
        yearMgr.bonusDetail({
          bonusDetailId: this.info.bonusDetailId,
          userId: this.info.userId
        }).then(res => {
          this.list = res
        })
      },
      show(info) {
        this.visible = true
        this.name = this.$route.name
        this.info = info
        this.$refs.info.init(new DetailOpt(), this.info)
        this.$refs.table.init(this, new DetailColumn(this.name, this.change))
        this.getList()
      },
      hide() {
        this.visible = false
      },
    },
  }
</script>

<style lang='scss' scoped>
</style>